package com.kexio.auth.dto;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.List;

/**
 * 数据权限信息DTO
 * 
 * 封装用户的数据权限范围配置，用于数据级权限控制
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DataScopeInfo implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 数据权限类型：ALL/DEPT/DEPT_AND_CHILD/SELF/CUSTOM
     */
    private String scopeType;
    
    /**
     * 用户可访问的部门ID列表（根据角色数据权限计算得出）
     */
    private List<String> deptIds;
    
    /**
     * 自定义部门ID列表（角色配置的自定义部门）
     */
    private List<String> customDeptIds;
    
    /**
     * 用户ID（用于SELF类型的数据权限）
     */
    private String userId;
    
    /**
     * 用户所属部门ID
     */
    private String userDeptId;
    
    /**
     * 用户部门路径（包含父级部门，用斜杠分隔，如：/1/2/3/）
     */
    private String deptPath;
    
    /**
     * 是否包含子部门数据
     */
    private Boolean includeChildDept;
    
    // ==================== 构造函数 ====================
    
    public DataScopeInfo() {}
    
    public DataScopeInfo(String scopeType, String userId) {
        this.scopeType = scopeType;
        this.userId = userId;
    }
    
    public DataScopeInfo(String scopeType, String userId, String userDeptId) {
        this(scopeType, userId);
        this.userDeptId = userDeptId;
    }
    
    // ==================== 便捷方法 ====================
    
    /**
     * 判断是否有全部数据权限
     */
    public boolean hasAllDataAccess() {
        return "ALL".equals(this.scopeType);
    }
    
    /**
     * 判断是否为部门数据权限
     */
    public boolean isDeptScope() {
        return "DEPT".equals(this.scopeType);
    }
    
    /**
     * 判断是否为部门及子部门数据权限
     */
    public boolean isDeptAndChildScope() {
        return "DEPT_AND_CHILD".equals(this.scopeType);
    }
    
    /**
     * 判断是否为个人数据权限
     */
    public boolean isSelfScope() {
        return "SELF".equals(this.scopeType);
    }
    
    /**
     * 判断是否为自定义数据权限
     */
    public boolean isCustomScope() {
        return "CUSTOM".equals(this.scopeType);
    }
    
    /**
     * 获取用于权限过滤的部门ID列表
     */
    public List<String> getFilterDeptIds() {
        if (isCustomScope() && customDeptIds != null && !customDeptIds.isEmpty()) {
            return customDeptIds;
        }
        return deptIds;
    }
    
    /**
     * 判断是否有部门访问权限
     */
    public boolean hasDeptAccess(String deptId) {
        if (hasAllDataAccess()) {
            return true;
        }
        
        List<String> filterDeptIds = getFilterDeptIds();
        if (filterDeptIds == null || filterDeptIds.isEmpty()) {
            return false;
        }
        
        return filterDeptIds.contains(deptId);
    }
    
    /**
     * 判断是否需要数据权限过滤
     */
    public boolean needDataFilter() {
        return !hasAllDataAccess();
    }
    
    // ==================== Getter/Setter ====================
    
    public String getScopeType() {
        return scopeType;
    }
    
    public void setScopeType(String scopeType) {
        this.scopeType = scopeType;
    }
    
    /**
     * 获取数据权限范围（与getScopeType等价，用于兼容性）
     */
    public String getDataScope() {
        return scopeType;
    }
    
    /**
     * 设置数据权限范围（与setScopeType等价，用于兼容性）
     */
    public void setDataScope(String dataScope) {
        this.scopeType = dataScope;
    }
    
    public List<String> getDeptIds() {
        return deptIds;
    }
    
    public void setDeptIds(List<String> deptIds) {
        this.deptIds = deptIds;
    }
    
    public List<String> getCustomDeptIds() {
        return customDeptIds;
    }
    
    public void setCustomDeptIds(List<String> customDeptIds) {
        this.customDeptIds = customDeptIds;
    }
    
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public String getUserDeptId() {
        return userDeptId;
    }
    
    public void setUserDeptId(String userDeptId) {
        this.userDeptId = userDeptId;
    }
    
    public String getDeptPath() {
        return deptPath;
    }
    
    public void setDeptPath(String deptPath) {
        this.deptPath = deptPath;
    }
    
    public Boolean getIncludeChildDept() {
        return includeChildDept;
    }
    
    public void setIncludeChildDept(Boolean includeChildDept) {
        this.includeChildDept = includeChildDept;
    }
    
    @Override
    public String toString() {
        return "DataScopeInfo{" +
                "scopeType='" + scopeType + '\'' +
                ", deptIds=" + deptIds +
                ", customDeptIds=" + customDeptIds +
                ", userId='" + userId + '\'' +
                ", userDeptId='" + userDeptId + '\'' +
                ", deptPath='" + deptPath + '\'' +
                ", includeChildDept=" + includeChildDept +
                '}';
    }
}
